package com.cater.sys.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.cater.annotation.SystemControllerLog;
import com.cater.sys.entity.Dictionarydata;
import com.cater.sys.entity.Resource;
import com.cater.sys.entity.Role;
import com.cater.sys.service.ResourceService;
import com.cater.sys.service.RoleService;
import com.cater.sys.service.impl.DictionarydataServiceImpl;

/**
 * 
 * @描述:角色管理
 * @作者 :ShenDong
 * @创建日期 :2016年12月7日8:41
 *
 */
@Controller
@RequestMapping("/role")
public class RoleController {

	@Autowired
	RoleService roleService;

	@Autowired
	ResourceService resourceService;
	
	@Autowired
	DictionarydataServiceImpl service;

	/**
	 * 
	 * @描述:打开角色信息
	 * @方法名称 :role
	 * @作者 :ShenDong
	 * @创建日期 :2016年12月7日 上午12:23:00
	 * @param request
	 * @param response
	 *
	 */
	@RequestMapping("/list")
	@SystemControllerLog(description = "查看角色")
	public ModelAndView role(ModelAndView mv, HttpServletRequest request) {
		List<Role> role = roleService.getAll();
		mv.addObject("role", role);
		mv.addObject("service", service);
		mv.setViewName("sys/role/role_list");
		return mv;
	}
	
	/**
	 * 打开角色添加页面
	 * @return
	 */
	@RequestMapping("/toAdd")
	public String toAdd(Model model){
		List<Dictionarydata> dictionarydata = service.getDataByType("role_type");
		model.addAttribute("dictionarydata", dictionarydata);
		return "sys/role/role_add";
	}
	
	/**
	 * 打开角色添加页面
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/add")
	@SystemControllerLog(description = "添加角色")
	public JSONObject add(Role role){
		role.setState("1");
		JSONObject json = new JSONObject();
		try {
			roleService.save(role);
			json.put("code", 200);
			json.put("message", "添加成功！");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "未知错误！");
		}
		return json;
	}
	
	
	/**
	 * 打开角色修改页面
	 * @return
	 */
	@RequestMapping("/toEdit")
	public ModelAndView toEdit(@RequestParam Integer id,ModelAndView mv){
		Role role = roleService.getbyid(id);
		mv.addObject("role", role);
		List<Dictionarydata> dictionarydata = service.getDataByType("role_type");
		mv.addObject("dictionarydata", dictionarydata);
		mv.setViewName("sys/role/role_edit");
		return mv;
	}
	
	/**
	 * 打开角色添加页面
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/edit")
	@SystemControllerLog(description = "修改角色")
	public JSONObject edit(Role role){
		role.setState("1");
		JSONObject json = new JSONObject();
		try {
			roleService.update(role);
			json.put("code", 200);
			json.put("message", "修改成功！");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "未知错误！");
		}
		return json;
	}
	
	/**
	 * 分配资源页面
	 */
	@RequestMapping(value = "/openauth")
	public String openauth(@RequestParam String id, Model model) throws Exception {
		model.addAttribute("roleId", id);
		return "sys/role/authorization";
	}

	/**
	 * 分配资源
	 */
	@ResponseBody
	@RequestMapping(value = "/auth")
	public List<Resource> auth(@RequestParam Integer id, Model model) throws Exception {
		List<Resource> resource = resourceService.getAll();
		List<Resource> resourcecked = resourceService.getAllc(id);
		for (Resource resource2 : resource) {
			resource2.setOpen("true");// 树默认展开
			for (Resource role : resourcecked) {
				if (resource2.getId().equals(role.getId())) {
					resource2.setChecked("true");
				}
			}
		}
		return resource;
	}

	/**
	 * 分配资源保存
	 */
	@ResponseBody
	@RequestMapping(value = "/auth/save")
	@SystemControllerLog(description = "分配资源")
	public JSONObject authsave(@RequestParam Integer roleId, @RequestParam String menuIds, Model model)
			throws Exception {
		JSONObject json = new JSONObject();
		String[] v_ids = menuIds.split(",");
		try {
			roleService.removeRR(roleId);
			for (String string : v_ids) {
				if(string.length()>0){
					Integer resourceId= Integer.valueOf(string);
					roleService.saveRR(roleId,resourceId);
				}
			}
			json.put("code", 200);
			json.put("message", "添加成功！");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "未知错误！");
		}
		return json;
	}

	/**
	 * 删除角色
	 */
	@ResponseBody
	@RequestMapping(value = "/del")
	@SystemControllerLog(description = "删除角色")
	public JSONObject del(@RequestParam Integer id){
		JSONObject json = new JSONObject();
		try {
			roleService.removeRR(id);
			roleService.delete(id);
			json.put("code", 200);
			json.put("message", "刪除成功！");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "未知错误！");
		}
		return json;
	}
}
